const { getBooksData, queryBooks } = require('../../libraryDB/books')
const { dataToWrite } = require('../../libraryDB/utils/getLogs')

const { app } = require('../utils/request')
const getBooksDataApi = async (req, res) => {
  try {
    // 取出请求体的数据getBooksData
    const data = req.body
    const dataRes = await getBooksData({ ...data })
    if (!dataRes.data.success) return res.status(401).json({ ...dataRes })
    res.status(200).json({ ...dataRes })
    console.log(dataRes.time, dataRes.msg)
    dataToWrite(`${JSON.stringify([req.url, dataRes.time, dataRes.msg])},`)
  } catch (error) {
    console.log(error)
    res.status(500).json({ ...dataRes })
  }
}

const queryBooksApi = async (req, res) => {
  try {
    const data = req.body
    // console.log(data, 111)
    const dataRes = await queryBooks({ ...data })
    // console.log(dataRes)
    if (!dataRes.data.success) return res.status(401).json({ ...dataRes })
    res.status(200).json({ ...dataRes })
    dataToWrite(`${JSON.stringify([req.url, dataRes.time, dataRes.msg])}`)
  } catch (error) {
    res.status(500).json({ ...dataRes })
  }
}
module.exports = { getBooksDataApi, queryBooksApi }
